<?php

namespace App\Http\Controllers\Admin;

use App\Model\Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $config = Config::get();

        foreach ($config as $k => $v) {
            switch ($v->field_type) {
                case 'input':
                    $v->conf_contents = '<input type="text" name="conf_content" id="L_conf_content" value="'.$v->conf_content.'" class="layui-input">';
                    break;
                
                case 'textarea':
                    $v->conf_contents = '<textarea name="conf_content" id="L_conf_content" class="layui-textarea">'.$v->conf_content.'</textarea>';
                    break;

                case 'radio':
                    $str = '';
                    $arr = explode(',', $v->field_value);

                    foreach ($arr as $m => $n) {
                        $info = explode('|', $n);
             
                        if ($info[0] == $v->conf_content) {
                            $str.= '<input type="radio" checked name="conf_content" id="L_conf_content" value="'.$info[0].'" title="'.$info[1].'">';
                        }else{
                            $str.= '<input type="radio" id="L_conf_content" name="conf_content" value="'.$info[0].'" title="'.$info[1].'">';
                        }
                    }

                    $v->conf_contents = $str;
                    break;
            }
        }

        $i = 1;
        return view('admin.config.list',compact('config','i'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.config.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $info = $request->all();

        //执行添加网站配置操作
        $res = Config::insert($info,true);
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $this->putContent();
            $data = [
                'status'=>0,
                'message'=>'添加成功'
            ];
        } else {
            $data = [
                'status'=>1,
                'message'=>'添加失败'
            ];
        }
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $config = Config::find($id);
        return view('admin.config.edit',compact('config'));        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $info = $request->all();
        //更新当前网站配置信息
        $res = Config::where('id', $id)
            ->update($info,true);
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $this->putContent();
            $data = [
                'status' => 0,
                'message' => '修改成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '修改失败'
            ];
        }
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = Config::where('id', $id)
            ->delete();
        //判断执行结果，将状态返回至页面
        if ($res) {
            # code...
            $data = [
                'status' => 0,
                'message' => '删除成功'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '删除失败'
            ];
        }
        return $data;
    }

    public function changeContent(Request $request)
    {
        $info = $request->all();
        // dd($info);

        DB::beginTransaction();

        try{
            foreach ($info['id'] as $k=>$v){
                $res = Config::where('id',$v)->
                    update(['conf_content'=>$info['conf_content'][$k]]);
            }

            DB::commit();
            $this->putContent();
            
            if ($res!=1) {
                # code...
                $this->putContent();
                $data = [
                    'status' => 0,
                    'message' => '修改成功'
                ];
            }else{
                $data = [
                    'status' => 1,
                    'message' => '修改失败'
                ];
            }
            return $data;

        }catch (\Exception $e){
            DB::rollBack();
            return redirect()->back()
                ->withErrors(['error'=>$e->getMessage()]);
        }
    }

    public function putContent()
    {
        // 从网站配置表中读取网站配置数据
        $content = Config::pluck('conf_content','conf_name')->all();

        // 准备要写入的内容
        $str = '<?php return '.var_export($content,true).';';

        // 将网站配置数据写入web.config.php文件
        file_put_contents(config_path().'/web.config.php', $str);

        // return success;
    }
}
